/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormAberturaComanda.java
 *
 * Created on 05/05/2013, 23:27:14
 */
package forms;

import classes.Conexao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Jonatas
 */
public class FormConsultarAberturaMesa extends javax.swing.JInternalFrame {

    /**
     * Creates new form FormAberturaComanda
     */
    public FormConsultarAberturaMesa() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelComanda = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableMesa = new javax.swing.JTable();

        setClosable(true);
        setTitle("Abertura de Mesa");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosed(evt);
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jLabelComanda.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabelComanda.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelComanda.setText("Mesas em Uso");

        jTableMesa.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mesas em Uso", "Data e Hora de Abertura", "Comanda"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Object.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTableMesa);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabelComanda, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabelComanda)
                .addGap(32, 32, 32)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-396)/2, (screenSize.height-307)/2, 396, 307);
    }// </editor-fold>//GEN-END:initComponents

    //Executa essas instruções ao abrir o internalFrame
    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened

        try {
            atualizarTabela();

        } catch (SQLException ex) {
            Logger.getLogger(FormConsultarAberturaMesa.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_formInternalFrameOpened

    //Executa essas instruções ao fechar o internalFrame
    private void formInternalFrameClosed(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosed
        //fazia desconexão com o banco, foi removido
    }//GEN-LAST:event_formInternalFrameClosed

    private void atualizarTabela() throws SQLException {
        DefaultTableModel tableModel = (DefaultTableModel) jTableMesa.getModel();
        tableModel.setRowCount(0);

        String sqlAbertMesa =
                "SELECT * "
                + "FROM \"ABERTURA_MESA\" "
                + "WHERE \"ABERTURA_MESA\".\"dt_hora_fechar\" is null "
                + "ORDER BY \"cd_mesa\"";

        ResultSet rs1 = Conexao.consultar(sqlAbertMesa);
        int mesa = 0;

        SimpleDateFormat dt = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        //System.out.println("Teste formato data: " + dt.format(new Date()));
        while (rs1.next()) {
            tableModel.addRow(new Object[]{
                        mesa = rs1.getInt("cd_mesa"),
                        dt.format(rs1.getTimestamp("dt_hora_abertura")),
                        rs1.getInt("cd_comanda")
                    });
        }

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabelComanda;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableMesa;
    // End of variables declaration//GEN-END:variables
}
